package com.hzy.yiyaoba.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hzy.yiyaoba.R;
import com.hzy.yiyaoba.R.id;
import com.hzy.yiyaoba.bean.LoreClass;

public class LoreClassListAdapter extends BaseAdapter implements OnClickListener{
	
	public static final String TAG = LoreClassListAdapter.class.getSimpleName();
	public static final int MSG_LORE_CLASS_SELECTED = 2001;

	private Context mContext;
	private LayoutInflater mInflater;
	private List<LoreClass> mLoreClasses;
	private Handler mHandler;

	public LoreClassListAdapter(Context context, Handler handler, List<LoreClass> loreClass) {
		mLoreClasses = loreClass;
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		mHandler = handler;
	}

	@Override
	public int getCount() {
		return mLoreClasses.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		TextView itemText;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.common_menu_list_item, null);
			holder = new ViewHolder();
			holder.itemText = (TextView) convertView.findViewById(id.common_menu_item_text);
			convertView.setTag(holder);
			convertView.setOnClickListener(this);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		LoreClass item = mLoreClasses.get(position);
		convertView.setTag(R.id.tag_second, item);
		holder.itemText.setText(item.getName());

		return convertView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_menu_list_area:
			LoreClass item = (LoreClass) v.getTag(R.id.tag_second);
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_LORE_CLASS_SELECTED;
			msg.obj = item;
			mHandler.sendMessage(msg);
			break;

		default:
			break;
		}
	}

}
